package uni.j2ee.forum.ejb.impl;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import uni.j2ee.forum.model.Category;
import uni.j2ee.forum.model.ForumTopic;

@Stateless
public class ForumTopicFacade extends AbstractFacade<ForumTopic> {

    @PersistenceContext(unitName = "SimpleForumJSF")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public ForumTopicFacade() {
        super(ForumTopic.class);
    }

    public List<ForumTopic> findByCategory(int[] range, Category category) {
        if (category != null) {
            return (List<ForumTopic>) em.createNamedQuery("ForumTopic.findByCategory").
                    setParameter("category", category).getResultList();
        } else {
            return (List<ForumTopic>) em.createNamedQuery("ForumTopic.findAll").getResultList();
        }
    }
}
